using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Chat : NetworkBehaviour
{
    public TMP_InputField chatText;//输入框
    public Button chatBtn;//发送按钮
    public GameObject chatInfo;//聊天框内容
    public GameObject chatFrame;//聊天框
    public PlayerControl playerControl;

    [SyncVar(hook = nameof(OnChatTextStringChanged))]
    public string chatTextString;

    private void OnChatTextStringChanged(string oldstr, string newstr)
    {
        Debug.Log(444);
        GameObject ci = Instantiate(chatInfo);
        ci.GetComponent<TMP_Text>().text = chatTextString;
        ci.transform.SetParent(chatFrame.transform);
    }

    void Awake()
    {
        
        chatBtn.onClick.AddListener(SendBtn);
    }

    public void SendBtn()
    {
        if (playerControl != null)
        {
            playerControl.CmdSendPLayerMessage(chatText.text);
        }
    }

    //同步切换场景
    public void ButtonChangeScene()
    {
        if (isServer)
        {
            var scene = SceneManager.GetActiveScene();
            NetworkManager.singleton.ServerChangeScene
            (
                scene.name == "SampleScene1" ? "SampleScene2" : "SampleScene1"
            );
        }
        else
        {
            Debug.Log("你不是房主");
        }
    }
}
